﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
	using UFIDA.U9.Base;
	using UFIDA.U9.CBO.MFG.WorkCenter;
	using UFIDA.U9.CBO.Pub.Controller;
	using UFIDA.U9.MO.MO;
	using UFIDA.U9.MO.SFC.DispatchCompleteOrder;
	using UFIDA.U9.MO.SFC.DispatchOrder;
	using UFIDA.U9.PM.Rcv;
	using UFSoft.UBF.AopFrame;
	using UFSoft.UBF.Business;
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// DispatchOrderSaveSV partial 
	/// </summary>	
	public partial class DispatchOrderSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new DispatchOrderSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class DispatchOrderSaveSVImpementStrategy : BaseStrategy
	{
		public DispatchOrderSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			DispatchOrderSaveSV bpObj = (DispatchOrderSaveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();

				foreach (var dto in bpObj.Data)
				{
					DispatchOrderDoc doc = DispatchOrderDoc.Finder.Find($"DocNo='{dto.DocNo}'");
					if (doc == null)
					{
                        //新增逻辑
                        doc = DispatchOrderBase.CreateDoc(dto);
                        //修改单号
                        EntityUtils.UpdateDocNo<DispatchOrderDoc>(doc.ID, dto.DocNo);
                        //审核
                        doc = DispatchOrderBase.ApproveDoc(doc);

                        //处理返回
                        retlist.Add(new CopyOfCommonArchiveDataDTOData()
						{
                            ID = doc.ID,
                            Code = doc.DocNo,
                        });
                    }
					else
					{
                        //修改逻辑
                        throw new AITException(599, $"单号:{dto.DocNo}已经存在,不能生成!");
                    }

				}

				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{

				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

        
    }

	#endregion
	
	
}